package com.basis.utils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.hibernate.Query;
import org.openbravo.dal.service.OBDal;

public class GetShopCalendarEndTime {
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	/**
	 * 根据开始时间、花费时间从工厂日历中计算结束时间
	 * @param fromTime   开始时间
	 * @param calendarId 工厂日历模板ID
	 * @param processTime所需时间
	 * @param timeUnit   时间单位
	 * @return
	 */
	public static synchronized Date getEndTime(Date fromTime, String calendarId, double processTime, String timeUnit) {
		String sql = "select Emes_Get_ShopCalendar_EndTime(?, ?, ?, ?) from dual";
		Query query = OBDal.getInstance().getSession().createSQLQuery(sql);
		Calendar date = Calendar.getInstance();
		date.setTime(fromTime);
		query.setCalendar(0, date);
		query.setString(1, calendarId);
		query.setBigDecimal(2, new BigDecimal(processTime));
		query.setString(3, timeUnit);
		return (Date)query.uniqueResult();
	}
	
	/**
	 * 根据开始时间、花费时间从工厂日历中计算结束时间
	 * @param fromTime   开始时间
	 * @param calendarId 工厂日历模板ID
	 * @param processTime所需时间
	 * @param timeUnit   时间单位
	 * @return
	 */
	public static synchronized String getEndTime(String fromTime, String calendarId, double processTime, String timeUnit) {
		String sql = "select Emes_Get_ShopCalendar_EndTime(?, ?, ?, ?) from dual";
		Query query = OBDal.getInstance().getSession().createSQLQuery(sql);
		query.setString(0, fromTime);
		query.setString(1, calendarId);
		query.setBigDecimal(2, new BigDecimal(processTime));
		query.setString(3, timeUnit);
		Date date = (Date)query.uniqueResult();
		if (date == null) {
			return null;
		}
		return dateFormat.format(date);
	}
}
